<template>
  <el-form :inline="true" class="demo-form-inline">
    <el-form-item label="一级分类" >
      <el-select
        v-model="categoryId1"
        placeholder="请输入"
        @change="getCategory2Id"
        :disabled="erha"
      >
        <el-option
          :label="category.name"
          :value="category.id"
          v-for="category in category1List"
          :key="category.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="二级分类" >
      <el-select
        v-model="categoryId2"
        placeholder="请输入"
        @change="getCategory3Id"
        :disabled="erha"
      >
        <el-option
          :label="c2.name"
          :value="c2.id"
          v-for="c2 in category2List"
          :key="c2.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="三级分类" >
      <el-select v-model="categoryId3" placeholder="请输入" @change="change3Id" :disabled="erha">
        <el-option
          :label="c3.name"
          :value="c3.id"
          v-for="c3 in category3List"
          :key="c3.id"
        ></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "categorySelect",
  data() {
    return {
      categoryId1: "",
      categoryId2: "",
      categoryId3: "",
      category1List: [],
      category2List: [],
      category3List: [],
      erha:false
    };
  },
  methods: {
    async getCategory1Id() {
      let result = await this.$api.category.getCategory1();
      if (result.code == 200) {
        this.category1List = result.data;
      }
    },
    //改变一级分类后获取二级分类
    async getCategory2Id(categoryId1) {
      this.categoryId2=null
       this.categoryId3=null
      this.category2List=[]
      this.category3List=[]
      this.$emit("getCategoryId", { lv: 1, id:categoryId1 });
      let result = await this.$api.category.getCategory2(categoryId1);
      if (result.code == 200) {
        this.category2List = result.data;
      }
    },
    //改变二级分类后获取三级分类
    async getCategory3Id(categoryId2) {
      this.categoryId3=null
      this.category3List=[]
      this.$emit("getCategoryId", { lv: 2, id:categoryId2 });
      let result = await this.$api.category.getCategory3(categoryId2);
      if (result.code == 200) {
        this.category3List = result.data;
      }
    },
    //改变三级分类后获取id
    change3Id(categoryId3) {
      this.$emit("getCategoryId", { lv: 3, id:categoryId3 });
    },
  },
  mounted() {
    this.getCategory1Id();
  },
};
</script>

<style scoped>
</style>